﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Gift.Model;
using Gift.DAL;

namespace WebSite.m
{
    public partial class index : System.Web.UI.Page
    {

        readonly DALCardType theCardType = new DALCardType();
        readonly Card theCard = new Card();
        Shoppro theShoppro = new Shoppro();
        readonly dalCar theDalCar = new dalCar();
        readonly Car theCar = new Car();
        readonly OrderAccount theOrderAccount = new OrderAccount();

        readonly Order theOrder = new Order();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindDayList();
                DateTime dtDate = DateTime.Now.AddDays(3);
                this.lblToDate.Text = DateTime.Now.AddDays(5).ToString("yyyy-MM-dd");
                this.ddlYear.Items.FindByValue(dtDate.Year.ToString()).Selected = true;
                this.ddlMonth.Items.FindByValue(dtDate.Month.ToString()).Selected = true;
                this.ddlDay.Items.FindByValue(dtDate.Day.ToString()).Selected = true;
            }

        }

        int ValidateCount
        {
            get
            {
                if (Session["validateCount"] != null)
                {
                    return int.Parse(Session["validateCount"].ToString());
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                Session["validateCount"] = value;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            this.divCardInfo.InnerHtml = string.Empty;
            this.divError.InnerHtml = string.Empty;
            ValidateCount++;

            if (ValidateCount >= 3)
            {
                if (ValidateCount == 3) this.divValidate.Visible = true;
                else
                {
                    if (string.Compare(Session["checkCode"].ToString().ToUpper(), this.txtValidateCode.Text.ToUpper(), true) != 0)
                    {
                        this.divError.InnerHtml = "验证码不正确！";
                        return;
                    }
                }
            }

            try
            {

                dbCard oCard;
                oCard = theCard.GetModel(this.txtCardCode.Text.Trim(), this.txtPassword.Text.Trim());
                if (oCard != null)
                {
                    if (oCard.states == 0)
                    {
                        this.divError.InnerHtml = "该卡还未发行！";
                    }
                    else if (oCard.states == 2)
                    {
                        this.divError.InnerHtml = "该卡已被使用！";
                    }
                    else if (oCard.states == 3)
                    {
                        this.divError.InnerHtml = "该卡已失效请联系管理员！";
                    }
                    else if (oCard.states == 4 || oCard.states == 6)
                    {
                        this.divError.InnerHtml = "该卡还未激活,只有激活后才能使用！";
                    }
                    else if (oCard.StartTime > DateTime.Now.Date)
                    {
                        this.divError.InnerHtml = "该卡还未生效！";
                    }
                    else if (oCard.endtime < DateTime.Now.Date)
                    {

                        this.divError.InnerHtml = "该卡已过有效时间，请联系管理员！";

                    }
                    else if (oCard.states == 1)
                    {
                        ValidateCount = 0;
                        tbCardType oCardType = theCardType.GetModel(oCard.cardtypeId);
                        this.divCardInfo.InnerHtml = string.Format("您使用的是{0}的礼品卡,请选取您要兑换的礼品！", oCardType.Name.ToString());
                        DataTable dt = theShoppro.GetListByCardCode(oCard.cardcode);
                        StringBuilder sbProduct = new StringBuilder();
                        this.radioProduct.Items.Clear();
                        this.radioProduct.DataTextField = "proname";
                        this.radioProduct.DataValueField = "proid";
                        this.radioProduct.DataSource = dt;
                        this.radioProduct.DataBind();
                        this.divInput.Visible = false;
                        this.divResults.Visible = true;

                        Session["mobileCode"] = this.txtCardCode.Text.Trim();
                        Session["mobilePass"] = this.txtPassword.Text.Trim();

                    }
                }
                else
                {
                    this.divError.InnerHtml = "卡号或密码不正确！";
                }
            }catch
            {
                 this.divError.InnerHtml = "卡号或密码不正确！";
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.divResultError.InnerHtml = "";
            this.divError.InnerHtml = string.Empty;

            if (this.radioProduct.SelectedItem == null)
            {
                this.divResultError.InnerHtml = "您还没有选取产品！";
                return;
            }
            if (this.txtLinker.Text.Trim() == "")
            {
                this.divResultError.InnerHtml = "联系人不能为空！";
                return;
            }
            if (this.txtMobile.Text.Trim() == "")
            {
                this.divResultError.InnerHtml = "联系电话不能为空！";
                return;
            }
          
            if(base.Request.Form["AddressArea1$ddlProvence"] == "选择省")
            {
                  this.divResultError.InnerHtml = "请选择省！";
                return;
            }
            if (base.Request.Form["AddressArea1$ddlArea"] == "选择地区")
            {
                this.divResultError.InnerHtml = "请选择地区！";
                return;
            }
            if (this.txtAddress.Text.Trim() == "")
            {
                this.divResultError.InnerHtml = "送货地址不能为空！";
                return;
            }

            DateTime dtSelectDate = Convert.ToDateTime(this.ddlYear.SelectedItem.Value + "-" + this.ddlMonth.SelectedItem.Value + "-" + this.ddlDay.SelectedItem.Value);
            if (DateTime.Parse(dtSelectDate.ToShortDateString()).Subtract(DateTime.Parse(DateTime.Now.ToShortDateString())).Days < 3)
            {
                this.divResultError.InnerHtml = "送货时间只能到三天之后！";
                return;
            }

            dbCard oCard;
            oCard = theCard.GetModel(Session["mobileCode"].ToString(),Session["mobilePass"].ToString());
            if (oCard.states == 1)
            {
                dbOrder oOrder = new dbOrder();
                tbCar oCar = new tbCar();

                oCar.OrderCode = theOrderAccount.GenerateMobileOrderCode();
                oCar.ProdNumber = 1;
                oCar.ProdPrice = oCard.cardprice;
                oCar.ProductId = int.Parse(this.radioProduct.SelectedItem.Value);
                oCar.UpdateDate = DateTime.Now;
                theCar.Add(oCar);

                int iOrderId = int.Parse(oCar.OrderCode);
                oOrder.orderid = iOrderId;
                oOrder.ordercode = oCar.OrderCode;
                oOrder.pay = 15;//付款方式为礼品卡
                oOrder.states = 0;
                oOrder.cname1 = this.txtLinker.Text.Trim();

                oOrder.phone1 = this.txtMobile.Text.Trim();
                oOrder.address1 = this.AddressArea1.GetProvenceArea() +  this.txtAddress.Text.Trim();
                oOrder.Senddate = dtSelectDate;
                oOrder.ordertime = DateTime.Now;
                oOrder.languages = 1;
                oOrder.cardprice = oCard.cardprice;
                oOrder.totalprice = oCard.cardprice.ToString();
               // oOrder.content = this.txtRemark.Text.Trim();
                theOrder.Add(oOrder);

                theOrderAccount.AddCardToCardAccount(oCar.OrderCode, oCard.cardid);
                theDalCar.AddTempToReal(oCar.OrderCode, iOrderId);
                theOrderAccount.UpdateComplateAccount(oCar.OrderCode);

                Session["mobileOrderCode"] = oCar.OrderCode;
                Session["mobileUserName"] = oOrder.cname1;
                Session["mobilePhone"] = oOrder.phone1;
                Session["mobileAddress"] =  oOrder.address1;
                Session["mobileProductName"] = this.radioProduct.SelectedItem.Text;
               // Session["mobileRemark"] = this.txtRemark.Text.Trim();
                Session["mobileSendDate"] = oOrder.Senddate.ToString("yyyy-MM-dd");
                Session["mobileToDate"] = this.lblToDate.Text;

                Response.Redirect("mobile_b.aspx");
            }
        }

        private void BindDayList()
        {
            DateTime dtDate = DateTime.Now.AddDays(3);

            int iDay = DateTime.DaysInMonth(dtDate.Year, dtDate.Month);

            for (int i = 1; i <= iDay; i++)
            {
                this.ddlDay.Items.Add(new ListItem(i.ToString()));
            }
        }

        protected void ddlDay_SelectedIndexChanged(object sender, EventArgs e)
        {
           this.lblToDate.Text = Convert.ToDateTime(this.ddlYear.SelectedItem.Value + "-" + this.ddlMonth.SelectedItem.Value + "-" + this.ddlDay.SelectedItem.Value).AddDays(2).ToString("yyyy-MM-dd");
        }
    }
}